package com.jet.goods.controller;

import com.jet.core.model.vo.PageVo;
import com.jet.core.result.ResultJson;
import com.jet.goods.domain.dto.SpecDto;
import com.jet.goods.domain.dto.SpecValueDto;
import com.jet.goods.domain.entity.GoodsSpec;
import com.jet.goods.domain.query.SpecKeyQuery;
import com.jet.goods.domain.vo.SpecKeyVo;
import com.jet.goods.service.IGoodsSpecService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/goods/spec")
public class SpecController {

    private final IGoodsSpecService goodsSpecService;

    public SpecController(IGoodsSpecService goodsSpecService) {
        this.goodsSpecService = goodsSpecService;
    }

    @PostMapping
    public ResultJson<Void> create(@RequestHeader long merchantNo,
                                   @RequestHeader long storeNo,
                                   @RequestHeader String accountId,
                                   @RequestBody @Validated SpecDto dto) {
        goodsSpecService.create(merchantNo, dto);
        return ResultJson.ok();
    }

    @PutMapping("/{id}")
    public ResultJson<Void> edit(@RequestHeader long merchantNo,
                                 @RequestHeader long storeNo,
                                 @RequestHeader String accountId,
                                 @PathVariable("id") String id,
                                 @RequestBody @Validated SpecDto dto) {
        goodsSpecService.edit(merchantNo, id, dto);
        return ResultJson.ok();
    }

    @PutMapping("/batch-edit")
    public ResultJson<Void> batchEdit(@RequestHeader long merchantNo,
                                      @RequestHeader long storeNo,
                                      @RequestHeader String accountId,
                                      @RequestBody List<SpecValueDto> dtos) {
        goodsSpecService.batchEdit(merchantNo, dtos);
        return ResultJson.ok();
    }

    @DeleteMapping("/{id}")
    public ResultJson<Void> delete(@RequestHeader long merchantNo,
                                   @RequestHeader long storeNo,
                                   @RequestHeader String accountId,
                                   @PathVariable("id") String id) {
        goodsSpecService.delete(merchantNo, id);
        return ResultJson.ok();
    }

    @GetMapping("/{id}")
    public ResultJson<GoodsSpec> info(@RequestHeader long merchantNo,
                                      @RequestHeader long storeNo,
                                      @RequestHeader String accountId,
                                      @PathVariable("id") String id) {
        return ResultJson.ok(goodsSpecService.getById(id));
    }

    @GetMapping("/list")
    public ResultJson<List<GoodsSpec>> valueInfo(@RequestHeader long merchantNo,
                                                 @RequestHeader long storeNo,
                                                 @RequestHeader String accountId,
                                                 SpecKeyQuery query) {
        return ResultJson.ok(goodsSpecService.findAll(merchantNo, query));
    }

    @GetMapping("/page")
    public ResultJson<PageVo<SpecKeyVo>> page(@RequestHeader long merchantNo,
                                              @RequestHeader long storeNo,
                                              @RequestHeader String accountId,
                                              SpecKeyQuery query) {
        PageVo<SpecKeyVo> result = goodsSpecService.page(merchantNo, query);
        return ResultJson.ok(result);
    }
}
